<p>字符串比较值大小应使用.equals（）方法，而不是使用相等（<code> == </code>）和不相等（<code>！= </code>）运算符比较。</p>
<h2>不规范的代码示例</h2>
<pre>
String str1 = "blue";
String str2 = "blue";
String str3 = str1;

if (str1 == str2)
{
  System.out.println("they're both 'blue'"); // 这不会打印，因为对象不同
}

if (str1 == "blue")
{
  System.out.println("they're both 'blue'");
}

if (str1 == str3)
{
  System.out.println("they're the same object"); // 这个会打印
}
</pre>

<h2>正确的代码示例</h2>
<pre>
String str1 = "blue";
String str2 = "blue";
String str3 = str1;

if (str1.equals(str2))
{
  System.out.println("they're both 'blue'");
}

if (str1.equals("blue"))
{
  System.out.println("they're both 'blue'");
}

if (str1 == str3)
{
  System.out.println("they're the same object"); // 这仍然可以打印，但是可能不是您要执行的操作
}
</pre>

<h2>更多资料:</h2>
<ul>
  <li><a href="http://cwe.mitre.org/data/definitions/595.html">MITRE, CWE-595</a> - Comparison of Object References Instead of Object Contents</li>
  <li><a href="http://cwe.mitre.org/data/definitions/597">MITRE, CWE-597</a> - Use of Wrong Operator in String Comparison</li>
  <li><a href="https://www.securecoding.cert.org/confluence/x/wwD1AQ">CERT, EXP03-J</a> - Do not use the equality operators when comparing values of boxed primitives</li>
</ul>
